package com.skyline.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.skyline.base.type.IdolType;
import com.skyline.common.bean.Page;
import com.skyline.common.util.Constant;
import com.skyline.common.util.ViewPaths;
import com.skyline.common.util.WebHelper;
import com.skyline.user.model.Fan;
import com.skyline.user.model.User;
import com.skyline.user.service.AttentionService;

@RequestMapping("/attention")
@Controller
public class AttentionController {
	private static final Log LOGGER = LogFactory.getLog(BasicUserController.class);

//	private @Value("${view.user.login}")
//	String loginView;
//	private @Value("${view.attention.fanslist}")
//	String attentionView;

	@Autowired
	private AttentionService attentionService;
	private @Value("${spot.test.pagesize}")
	int attentionPageSize;


	/**
	 * 添加关注
	 * 
	 * @param idolId
	 * @param idolNickname
	 * @param idolPortrait
	 * @param idolType
	 * @return
	 */
	@RequestMapping(value = "/payattention", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> ajaxPayattention(Long idolId, String idolNickname,
			String idolPortrait, IdolType idolType) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("loginStatus", false);

		try {
			User user = (User) WebHelper.getSessionAttribute(null,
					Constant.SESSION_USER);
			if (user == null) {
				map.put("loginStatus", false);
				return map;
			}
			if (attentionService.isFan(idolId, user.getId())) {
				
				return null;
			}
			attentionService.payAttention(idolId, idolNickname, idolPortrait,
					user.getId(), user.getNickname(), user.getPortrait(),
					idolType);
			map.put("loginStatus", true);
			return map;

		} catch (Exception e) {
			LOGGER.error("添加关注失败:", e);
			return null;
		}

	}

	/**
	 * 取消关注
	 * 
	 * @param idolId
	 * @return
	 */
	@RequestMapping(value = "/delattention", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> ajaxdelAttention(Long idolId, IdolType idolType) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			User user = (User) WebHelper.getSessionAttribute(null,
					Constant.SESSION_USER);
			if (user == null) {
				map.put("loginStatus", false);
				return map;
			}
			if (attentionService.isFan(idolId, user.getId())) {
				System.out.println("已经关注");
				attentionService.cancelAttention(idolId, user.getId());
				System.out.println("估计删除成功");
				map.put("loginStatus", true);
				map.put("IsFanFlag", false);
				map.put("FansCount", attentionService.countFans(idolId));
				return map;
			} else {

				return null;
			}

		} catch (Exception e) {
			LOGGER.error("删除关注失败:", e);
			return null;
		}
	}

	/**
	 * 判断是否已关注
	 * 
	 * @param idolId
	 * @return
	 */
	@RequestMapping(value = "/isattention", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> ajaxisAttention(Long idolId, IdolType idolType) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			User user = (User) WebHelper.getSessionAttribute(null,
					Constant.SESSION_USER);
			if (user == null) {
				map.put("UserStatus", false);
				map.put("IsFanFlag", false);
				return map;
			} else if (idolId.equals(user.getId())) {
				map.put("UserStatus", "Me");
				return map;
			} else if (attentionService.isFan(idolId, user.getId())) {
				/**
				 * session 设置
				 */
				// WebHelper.setSessionAttribute(null,
				// Constant.SESSION_ATTENTION_FLAG, true);
				map.put("UserStatus", true);
				map.put("IsFanFlag", true);
				map.put("FansCount", attentionService.countFans(idolId));
				return map;
			} else {
				/**
				 * session 设置List<Fan>
				 * fanList=attentionService.queryFans(ownerId,page);
				 */
				// WebHelper.setSessionAttribute(null,
				// Constant.SESSION_ATTENTION_FLAG, false);
				map.put("UserStatus", true);
				map.put("IsFanFlag", false);
				map.put("FansCount", attentionService.countFans(idolId));
				return map;
			}

		} catch (Exception e) {
			LOGGER.error("获取关注失败:", e);
			return null;
		}
	}

	/**
	 * 查看你的粉丝
	 * 
	 * @param ownerId
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/{ownerId}/fans", method = RequestMethod.GET)
	public ModelAndView getfans(@PathVariable("ownerId") Long ownerId, Page page) {
		ModelAndView v = new ModelAndView(ViewPaths.USER_LOGIN);
		page.setSize(attentionPageSize);
		List<Fan> fanList = attentionService.queryFans(ownerId, page);
		v.setViewName(ViewPaths.USER_FANS);
		v.addObject("listType", 0);
		v.addObject("list", fanList);
		v.addObject("page", page);

		return v;
	}

	/**
	 * 查看你关注的东西
	 * 
	 * @param ownerId
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/{ownerId}/follow", method = RequestMethod.GET)
	public ModelAndView getAttention(@PathVariable("ownerId") Long ownerId,
			Page page) {
		ModelAndView v = new ModelAndView(ViewPaths.USER_LOGIN);
		page.setSize(attentionPageSize);
		List<Fan> attentionList = attentionService.queryAttentionByType(
				ownerId, null, page);
		v.setViewName(ViewPaths.USER_FOLLOW);
		v.addObject("listType", 0);
		v.addObject("list", attentionList);
		v.addObject("page", page);

		return v;
	}

	/**
	 * 根据关注的类别分类，因为涉及到景点什么，可能要用
	 * 
	 * @param ownerId
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/{ownerId}/follow/{type}", method = RequestMethod.GET)
	public ModelAndView getAttention(@PathVariable("ownerId") Long ownerId,
			@PathVariable("type") IdolType type, Page page) {
		ModelAndView v = new ModelAndView(ViewPaths.USER_LOGIN);
		page.setSize(attentionPageSize);
		List<Fan> attentionList = attentionService.queryAttentionByType(
				ownerId, type, page);
		v.setViewName(ViewPaths.USER_LOGIN);
		v.addObject("listType", type);
		v.addObject("list", attentionList);
		v.addObject("page", page);

		return v;
	}

}
